<?php

/**
 * BH 日历单表model
 * @author perry
 */

namespace Home\Model;

use Think\Model;

class BHCalendarModel extends Model {


    /**
     * 初始化
     */
    function _initialize() {
        $this->tableName = 'bh_calendar';
        $this->trueTableName  = 'eosi_bh_calendar';
    }


    /**
     * 添加一条日历数据,如果同用户同日已有旧数据，要先删除
     */
    function addrecommend($uid,$date,$look_id,$memo,$status,$is_top){
    	if(!ic_isPosInt($uid)){
    		throw new \Exception("添加人id不正确，请检查", -30);
    	}
    	if(!ic_isPosInt($look_id)){
    		throw new \Exception("造型id不正确，请检查", -30);
    	}
    	$aa=$status+2;
    	if(!ic_isPosInt($aa)){
    		throw new \Exception("状态值不正确，请检查", -30);
    	}
    	$aa=$is_top+1;
    	if(!ic_isPosInt($aa)){
    		throw new \Exception("是否置顶值不正确，请检查", -30);
    	}
    	$timeStamp=ic_strToTimeStamp($date);
    	if(!$timeStamp){
    		throw new \Exception("指定日期格式不正确，请检查", -30);
    	}
    	$fdate=ic_dateToStr($timeStamp,"Y-m-d");
    	$taa=explode("-", $fdate);
    	$addData=array();
    	$addData['uid']=$uid;
    	$addData['year']=$taa[0];
    	$addData['month']=$taa[1];
    	$addData['day']=$taa[2];
    	$addData['look_id']=$look_id;
    	$addData['memo']=ic_filterStr($memo);
    	$addData['update_time']=time();
    	$addData['status']=$status;
    	$addData['is_top']=$is_top;
        //先删除该日的其它官方推荐
        $this->deleteByDate($taa[0],$taa[1],$taa[2],C('super_style_uid'));

    	//如果一个造型只能推荐到某一天，则删除该造型id在日历里的所有数据
    	$this->deleteByLookId($look_id);
    	//先判断指定日是否已有灵感推荐造型，如果有的话，直接更新
    	// $map=array();
    	// $map['uid']=array('in',C('super_style_uid'));
    	// $map['year']=$taa[0];
    	// $map['month']=$taa[1];
    	// $map['day']=$taa[2];
    	// $exist=$this->where($map)->find();
    	// // dump($this->getLastSql());
    	// // dump($exist);
    	// // exit;
    	// if($exist){
    	// 	$this->where($map)->delete();
    	// 	//$flag=$this->where('id='.$exist['id'])->save($addData);
    	// 	//return $exist['id'];
    	// }
    	
    	$addData['create_time']=time();
    	
    	$id=$this->add($addData);
    	return $id;

    }

    //根据造型id查找是否有该造型添加到日历里的数据
    function findByLookId($look_id,$uarray=false){
        $map=array();
        $map['look_id']=$look_id;
        if($uarray){
            $map['uid']=array('in',$uarray);
        }
    	$re = $this->where($map)->find();
    	return $re;
    }

    //根据造型id删除日历表里使用的数据
    function deleteByLookId($look_id){
    	$re = $this->where('look_id='.$look_id)->delete();
    	return $re;
    }

    function deleteByDate($year,$month,$day,$uarray){
        $map=array();
        $map['year']=$year;
        $map['month']=$month;
        $map['day']=$day;
        $map['uid']=array('in',$uarray);
        $re = $this->where($map)->delete();
        return $re;
    }
    
    

    

}
